package com.my.controller;


import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.my.pojo.Goods;
import com.my.pojo.Stock;
import com.my.service.IStockService;
import com.my.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("/stock")
public class StockController {
    private IStockService iStockService;

    @Autowired
    public void setIStockService(IStockService iStockService) {
        this.iStockService = iStockService;
    }

    private void putMessage(Map<String, String> map, String code, String message) {
        map.put("code", code);
        map.put("message", message);
    }

    @RequestMapping("/in")
    public String in(Integer goods_id, Integer orders_id, Integer type, Integer num) {
        Map<String, String> map = new HashMap<>();
        Stock stock = new Stock();
        if (goods_id != -1) {
            Goods goods = new Goods();
            goods.setGoodsId(orders_id);
            stock.setGoods(goods);
            stock.setType(type);
            stock.setNum(num);
            Integer in = this.iStockService.in(stock);
            if (in != null) {
                this.putMessage(map, "200", "SUCCESS");
            } else {
                this.putMessage(map, "stock_500", "error");
            }
        }
        return new Gson().toJson(map);
    }

    /**
     * @param context     搜索内容 可为空
     * @param currentPage 当前第几页
     * @param pageSize    每页显示多少个
     * @param sort        排序列
     * @param asc         升序 降序
     * @param businessId  被查找的商家的id
     * @return 返回分页数据以及结果数据集
     */
    @RequestMapping("/showAll")
    public String showAll(String context,
                          Integer currentPage,
                          Integer pageSize,
                          String sort,
                          Boolean asc,
                          HttpServletRequest req) {
        String token = req.getHeader("token");
        Integer businessId = JWTUtils.getId(token);
        Map<String, String> map = new HashMap<>();
        Pageable pageable;
        if (!"".equals(sort)) {
            Sort.Order order = new Sort.Order((asc ? Sort.Direction.ASC : Sort.Direction.DESC), sort);
            Sort s = new Sort(order);
            pageable = new PageRequest(currentPage, pageSize, s);
        } else {
            pageable = new PageRequest(currentPage, pageSize);
        }

        Page<Stock> stockPage = this.iStockService.showAll(pageable, context, businessId);
        if (stockPage == null) {
            this.putMessage(map, "500", "error");
        } else {
            this.putMessage(map, "200", "success");
            map.put("totalNum", stockPage.getTotalElements() + "");
            map.put("totalPages", stockPage.getTotalPages() + "");
            map.put("history", listArrayToJson(stockPage.getContent()));
        }
        return new Gson().toJson(map);
    }

    private String listArrayToJson(List<Stock> list) {
        JsonArray jsonArray = new JsonArray();
        for (Stock stock : list) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("stockId", stock.getStockId());
            jsonObject.addProperty("goodsName", stock.getGoods().getName());
            jsonObject.addProperty("type", stock.getType());
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            jsonObject.addProperty("date", f.format(stock.getDate()));
            jsonObject.addProperty("num", stock.getNum());
            jsonObject.addProperty("info", stock.getInfo());
            jsonObject.addProperty("status", stock.getStatus());
            if (stock.getOrders() != null)
                jsonObject.addProperty("ordersId", stock.getOrders().getOrderId());
            else
                jsonObject.addProperty("ordersId", "");
            jsonArray.add(jsonObject);
        }
        return jsonArray.toString();
    }
}
